<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/20
 * Time: 下午7:47
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class ArtcateController extends AdminBaseController{
    /**
     * 文章分类列表页
     */
    public function index(){
        $this -> display();
    }

    /**
     * laypage jquery.ajax 异步请求分页
     */
    public function pageList(){
        $page = I('get.page/d',1);

        $category = M('category');

        // 查询满足要求的总记录数
        $count = $category -> where(['type' => 1]) -> count();

        $limit = 8;
        $pages = ceil($count / $limit);
        $start = empty($page - 1) ? 0 : (($page - 1) * $limit);

        $list = $category -> where(['type' => 1]) -> order('id') -> limit($start.','.$limit) -> select();
        $list = get_tree($list, 0, 0, '----&nbsp;');
        //  传给laypage分页js 的列表内容
        $content = '';
        foreach($list as $value){
            $content .= '<tr><td>' . $value['id'] . '</td><td>' . $value['html'] . $value['title'] . '</td><td>' . $value['columns'] . '</td><td>' . $value['seo_title'] . '</td><td>' . $value['seo_keyword'] . '</td><td>' . $value['seo_description'] . '</td><td>' . $value['px'] . '</td><td>';
            if($value['status']){
                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-primary" id="status' . $value['id'] . '">正常</a>';
            }else{
                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-danger" id="status' . $value['id'] . '">禁用</a>';
            }
            $content .= '</td><td><div class="col-md-12"><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Artcate/add/pid/' .$value['id']) . '"><i class="fa fa-edit"></i>添加子栏目</a><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/artcate/edit/id/' .$value['id']) . '"><i class="fa fa-edit"></i> 修改</a><a class="btn btn-sm btn-outline btn-danger" href="javascript:del(' . $value['id'] . ')"><i class="fa fa-trash-o"></i> 删除</a></div></td></tr>';
        }
        $data = [
            'pages' => $pages,
            'content' => $content,
        ];
        echo json_encode($data);
    }

    public function add(){
        $pid = I('get.pid/d',0);
        $this -> assign('pid', $pid);
        $this -> display();
    }
    public function edit(){
        if(!IS_GET){
            $this -> error('非法操作',"/Admin/Artcate/index");
            exit;
        }
        $id = I('get.id/d',0);
        if(empty($id)){
            $this -> error('非法操作',"/Admin/Artcate/index");
            exit;
        }
        $category = M('category');
        $categoryInfo = $category -> where(['id' => $id]) -> find();

        $this -> assign('artcateInfo', $categoryInfo);
        $this -> display('artcate_add');
    }

    public function Save(){
        if(IS_POST && IS_AJAX){
            $id = I('post.id/d',0);
            $pid = I('post.pid/d',0);
            $title = I('post.title/s','');
            $columns = I('post.columns/s','');
            $seo_title = I('post.seo_title/s','');
            $seo_keyword = I('post.seo_keyword/s','');
            $seo_description = I('post.seo_description/s','');
            $px = I('post.px/d',0);
            $status = I('post.status/d',0);

            //  验证
            if(!$columns){
                $this -> ajaxReturn(['status' => 1, 'msg' => '请输入英文名称']);
            }
            if(!$seo_title){
                $this -> ajaxReturn(['status' => 1, 'msg' => '请输入SEO标题']);
            }
            if(!$seo_keyword){
                $this -> ajaxReturn(['status' => 1, 'msg' => '请输入SEO关键词']);
            }
            if(!$seo_description){
                $this -> ajaxReturn(['status' => 1, 'msg' => '请输入SEO描述']);
            }
            if(!$px){
                $this -> ajaxReturn(['status' => 1, 'msg' => '请输入文章栏目的排序']);
            }

            if(empty($id)){
                /**
                 * ID为空即为添加
                 */
                //  验证栏目名称
                if(!$title){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '栏目名称不能为空']);
                }
                $category = M('category');
                $r = $category ->field('id') -> where(['title' => $title, 'type' => 1]) -> find();
                if(!is_null($r)){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '栏目名称已被占用']);
                }

                $data = [
                    'pid' => $pid,
                    'type' => 1,
                    'title' => $title,
                    'columns' => $columns,
                    'seo_title' => $seo_title,
                    'seo_keyword' => $seo_keyword,
                    'seo_description' => $seo_description,
                    'px' => $px,
                    'status' => $status
                ];
                $isAdd = $category -> add($data);

                if(!$isAdd){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '添加失败']);
                }
                $this -> ajaxReturn(['status' => 0, 'msg' => '添加成功']);
            }else{
                /**
                 * ID不为空即为修改
                 */

                //  验证栏目名称
                if(!$title){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '栏目名称不能为空']);
                }
                $category = M('category');
                //  通过提交的title 查询数据库
                $r = $category ->field('id') -> where(['title' => $title, 'type' => 1]) -> find();
                //  如果$r不为空则说明提交的title已存在
                if(!is_null($r)){
                    if($r['id'] != $id){
                        $this -> ajaxReturn(['status' => 1, 'msg' => '栏目名称已被占用']);
                    }
                }


                $data = [
                    'title' => $title,
                    'columns' => $columns,
                    'seo_title' => $seo_title,
                    'seo_keyword' => $seo_keyword,
                    'seo_description' => $seo_description,
                    'px' => $px,
                    'status' => $status
                ];

                $isUp = $category -> where(['id' => $id]) -> setField($data);
                if(0 === $isUp){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '别闹...你没有修改任何内容']);
                }elseif(false === $isUp){
                    $this -> ajaxReturn(['status' => 1, 'msg' => '修改失败']);
                }
                $this -> ajaxReturn(['status' => 0, 'msg' => '修改成功']);
            }
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);

        }
    }

    /**
     * 更改栏目状态
     */
    public function statusChange(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
            $category = M('category');
            $r = $category ->field('status') -> where(['id' => $id, 'type' => 1]) -> find();
            if(1 == $r['status']){
                $category -> where(['id' => $id]) -> setField('status', 0);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已禁用', 'data' => 0]);
            }else{
                $category -> where(['id' => $id]) -> setField('status', 1);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已开启', 'data' => 1]);
            }
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }

    /**
     * 删除栏目
     */
    public function del(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d');
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }

            $category = M('category');
            $isDel = $category -> delete($id);

            if(!$isDel){
                $this -> ajaxReturn(['status' => 1, 'msg' => '删除失败']);
            }
            $this -> ajaxReturn(['status' => 0, 'msg' => '删除成功']);
        }else{
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
        }
    }
}